//OR36 链表中的回文结构

public class PalindromeList {
    public boolean chkPalindrome(ListNode A) {
        // write code here
        ListNode fast = A;
        ListNode slow = A;
        //找到中间的位置 - 前面的是偶数判断，后面是奇数判断
        //两者不能互换，因为fast可能为null，会报错
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        //将后半段反转
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curNext = cur.next;//记录cur.next
            cur.next = slow;//翻转
            slow = cur;//向后移
            cur = curNext;
        }
        //比较两者的值是否相等 - 前面是奇数的判断，后面是偶数的判断
        while(slow != A && slow != A.next){
            if(slow.val != A.val){
                return false;
            }
            slow = slow.next;
            A = A.next;
        }
        return true;
    }
}
//160 —— 相交链表
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode curA = headA;
        while(curA != null){
            ListNode curB = headB;
            while(curB != null){
                if(curB ==curA){
                    return curA;
                }
                curB = curB.next;
            }
            curA = curA.next;
        }
        return null;
    }
}
//21 - 合并两个有序链表
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode newHead = new ListNode();
        ListNode tmpHead = newHead;
        while(list1 != null && list2 != null){
            if(list1.val > list2.val){
                tmpHead.next = list2;
                list2 = list2.next;
            }else{
                tmpHead.next = list1;
                list1 = list1.next;
            }
            tmpHead = tmpHead.next;
        }
        if(list1 != null){
            tmpHead.next = list1;
        }
        if(list2 != null){
            tmpHead.next = list2;
        }
        return newHead.next;
    }
}
//链表中倒数第K个节点
 class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(k <= 0 || head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        for(int i = 0; i < k-1; i++){
            fast = fast.next;
            if(fast == null){
                return null;
            }
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
//876 - 链表中的中间节点
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}
//206 —— 反转链表
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null) return null;
        ListNode cur = head.next;
        head.next = null;//注意要将原来第一个置为null !!!!!
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
}

//203 - 移除链表元素
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) return null;
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == val){
                prev.next = cur.next;
            }else{
                prev = cur;
            }
            cur = cur.next;
        }
        if(head.val == val){
            head = head.next;
        }
        return head;
    }
}

//CM11 - 链表分割
class Partition {
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;
        while(cur != null){
            if(bs == null && cur.val < x){
                bs = be = cur;
            }else if(as == null && cur.val >= x){
                as = ae = cur;
            }else if(cur.val < x){
                be.next = cur;
                be = be.next;
            }else if(cur.val >= x){
                ae.next = cur;
                ae = ae.next;
            }
            cur = cur.next;
        }
        if(bs == null){//防止值全都大于X
            return as;
        }
        if(as != null){
            ae.next = null;//将最后一个置为空
        }
        be.next = as;//将前后链接起来
        return bs;
    }
}
public class Test {
}
